package com.inqbarna.tablefixheaders.samples.adapters;

import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;
import com.inqbarna.tablefixheaders.samples.ResourceTable;
import com.inqbarna.tablefixheaders.utils.ResUtils;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.TypedAttribute;

public class MatrixTableAdapter<T> extends BaseTableAdapter {
    private final static int WIDTH_DIP = 300;

    private final static int HEIGHT_DIP = 100;

    private final Context context;

    private T[][] table;

    private int width;

    private int height;

    private final int FONT_SIZE = 40;

    public MatrixTableAdapter(Context context) {
        this(context, null);
    }

    public MatrixTableAdapter(Context context, T[][] table) {
        this.context = context;
        ResourceManager r = context.getResourceManager();
        try {
            width = Math.round(TypedAttribute.computeFloatValue(String.valueOf(WIDTH_DIP), r.getConfiguration(),
                r.getDeviceCapability()));
            height = Math.round(TypedAttribute.computeFloatValue(String.valueOf(HEIGHT_DIP), r.getConfiguration(),
                r.getDeviceCapability()));
        } catch (WrongTypeException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        setInformation(table);
    }

    public void setInformation(T[][] table) {
        this.table = table;
    }

    @Override
    public int getRowCount() {
        return table.length - 1;
    }

    @Override
    public int getColumnCount() {
        return table[0].length - 1;
    }

    @Override
    public Component getView(int row, int column, Component convertView, ComponentContainer parent) {
        if (convertView == null) {
            convertView = new Text(context);
            ((Text) convertView).setTextAlignment(TextAlignment.LEFT);
            ((Text) convertView).setTextSize(FONT_SIZE);
            ((Text) convertView).setBackground(
                ResUtils.getShapeElement(context, ResUtils.ANGLE_0, ResourceTable.Color_simple_table_bg_color));
        }
        ((Text) convertView).setText(table[row + 1][column + 1].toString());
        return convertView;
    }

    @Override
    public int getHeight(int row) {
        return height;
    }

    @Override
    public int getWidth(int column) {
        return width;
    }

    @Override
    public int getItemViewType(int row, int column) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }
}
